package com.eight.vote.util;

import lombok.Data;

/**
 * @Author 点点星言
 * @title my-book
 * @Describe 全局统一结果返回
 * @time 2022-09-09  09:27
 */
@Data
public class Result<T> {

    private Integer code;
    private String message;
    private T Data;
    private String success;

    /** 空参构造 */
    public Result(){}

    /**
     * 快速构建返回结果
     * @param body
     * @param code
     * @param message
     * @param <T>
     * @return
     */
    public static <T> Result<T> build(T body, Integer code, String message,String success) {
        /* 创建结果类 */
        Result<T> result = new Result<T>();
        if (body != null) {
            /* 传入的数据不为空时将其设置到数据属性中 */
            result.setData(body);
        }
        result.setCode(code);
        result.setMessage(message);
        return result;
    }
    public static<T> Result<T> ok(){
        return Result.ok(null);
    }

    /**
     * 操作成功
     * @param data  baseCategory1List
     * @param <T>
     * @return
     */
    public static<T> Result<T> ok(T data){
        return build(data,200,"很单纯的成功了","成功");
    }

    public static<T> Result<T> fail(){
        return Result.fail(null);
    }

    /**
     * 操作失败
     * @param msg
     * @param <T>
     * @return
     */

    public static<T> Result<T> fail(String msg){
        return build(null,201,msg,"失败");
    }

    public Result<T> message(String msg){
        this.setMessage(msg);
        return this;
    }

    public Result<T> code(Integer code){
        this.setCode(code);
        return this;
    }


}
